package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.CountryStrichenService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.CountryStrichenDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CountryStrichen.
 */
@RestController
@RequestMapping("/api")
public class CountryStrichenResource {

    private final Logger log = LoggerFactory.getLogger(CountryStrichenResource.class);

    private static final String ENTITY_NAME = "countryStrichen";

    private final CountryStrichenService countryStrichenService;

    public CountryStrichenResource(CountryStrichenService countryStrichenService) {
        this.countryStrichenService = countryStrichenService;
    }

    /**
     * POST  /country-strichens : Create a new countryStrichen.
     *
     * @param countryStrichenDTO the countryStrichenDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new countryStrichenDTO, or with status 400 (Bad Request) if the countryStrichen has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/country-strichens")
    @Timed
    public ResponseEntity<CountryStrichenDTO> createCountryStrichen(@RequestBody CountryStrichenDTO countryStrichenDTO) throws URISyntaxException {
        log.debug("REST request to save CountryStrichen : {}", countryStrichenDTO);
        if (countryStrichenDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new countryStrichen cannot already have an ID")).body(null);
        }
        CountryStrichenDTO result = countryStrichenService.save(countryStrichenDTO);
        return ResponseEntity.created(new URI("/api/country-strichens/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /country-strichens : Updates an existing countryStrichen.
     *
     * @param countryStrichenDTO the countryStrichenDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated countryStrichenDTO,
     * or with status 400 (Bad Request) if the countryStrichenDTO is not valid,
     * or with status 500 (Internal Server Error) if the countryStrichenDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/country-strichens")
    @Timed
    public ResponseEntity<CountryStrichenDTO> updateCountryStrichen(@RequestBody CountryStrichenDTO countryStrichenDTO) throws URISyntaxException {
        log.debug("REST request to update CountryStrichen : {}", countryStrichenDTO);
        if (countryStrichenDTO.getId() == null) {
            return createCountryStrichen(countryStrichenDTO);
        }
        CountryStrichenDTO result = countryStrichenService.save(countryStrichenDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, countryStrichenDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /country-strichens : get all the countryStrichens.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of countryStrichens in body
     */
    @GetMapping("/country-strichens")
    @Timed
    public ResponseEntity<List<CountryStrichenDTO>> getAllCountryStrichens(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of CountryStrichens");
        Page<CountryStrichenDTO> page = countryStrichenService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/country-strichens");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /country-strichens/:id : get the "id" countryStrichen.
     *
     * @param id the id of the countryStrichenDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the countryStrichenDTO, or with status 404 (Not Found)
     */
    @GetMapping("/country-strichens/{id}")
    @Timed
    public ResponseEntity<CountryStrichenDTO> getCountryStrichen(@PathVariable Long id) {
        log.debug("REST request to get CountryStrichen : {}", id);
        CountryStrichenDTO countryStrichenDTO = countryStrichenService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(countryStrichenDTO));
    }

    /**
     * DELETE  /country-strichens/:id : delete the "id" countryStrichen.
     *
     * @param id the id of the countryStrichenDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/country-strichens/{id}")
    @Timed
    public ResponseEntity<Void> deleteCountryStrichen(@PathVariable Long id) {
        log.debug("REST request to delete CountryStrichen : {}", id);
        countryStrichenService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/country-strichens?query=:query : search for the countryStrichen corresponding
     * to the query.
     *
     * @param query the query of the countryStrichen search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/country-strichens")
    @Timed
    public ResponseEntity<List<CountryStrichenDTO>> searchCountryStrichens(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of CountryStrichens for query {}", query);
        Page<CountryStrichenDTO> page = countryStrichenService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/country-strichens");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
